package com.jinbai.servlet.album;

import com.jinbai.repo.AlbumRepo;
import com.jinbai.util.Log;
import com.jinbai.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/studio/album/create.do")
public class CreateAlbumDoServlet extends HttpServlet {
    private final AlbumRepo albumRepo = new AlbumRepo();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserVO userVO = null;
        final HttpSession session = req.getSession(false);
        if (session != null) {
            userVO = (UserVO) session.getAttribute("currentUser");
        }
        if (userVO == null) {
            Log.println("用户未登录");
            resp.sendRedirect("/studio/user/login.html");
            return;
        }

        req.setCharacterEncoding("utf-8");
        final String title = req.getParameter("title");
        final String coverUrl = req.getParameter("coverUrl");
        int state = 1;
        albumRepo.insertAlbum(userVO, title, coverUrl, state);
        resp.sendRedirect("/studio/album/list.html");
    }
}
